package com.photoeditor.demo.model.mlkit;

import android.graphics.Rect;

import com.common.base.utils.CryptTool;
import com.google.firebase.ml.vision.face.FirebaseVisionFace;
import com.google.firebase.ml.vision.face.FirebaseVisionFaceContour;

/**
 * Created by @author winn on 2018/12/19.
 */
public class LandmarkTransform {

    private static final byte[] LANDMARK_ENCRYPT_KEY = new byte[]{88, 83, 84, 54, 74, 113, 89, 114, 103};

    private int mViewWidth;
    private int mViewHeight;
    private int mPreviewWidth;
    private int mPreviewHeight;
    private boolean mIsFrontCamera = true;
    private float widthScaleFactor;
    private float heightScaleFactor;

    public LandmarkTransform(){

    }

    public void setViewSize(int width, int height) {
        mViewWidth = width;
        mViewHeight = height;
        resetData();
    }

    public void setCamera(int previewWidth, int previewHeight, boolean isFrontCamera) {
        mPreviewWidth = previewWidth;
        mPreviewHeight = previewHeight;
        mIsFrontCamera = isFrontCamera;
        resetData();
    }

    private void resetData() {
        if (mPreviewWidth == 0 || mPreviewHeight == 0) {
            return;
        }
        widthScaleFactor = (float) mViewWidth / (float) mPreviewWidth;
        heightScaleFactor = (float) mViewHeight / (float) mPreviewHeight;
    }

    /**
     * 将mlkit中识别的人脸框转换成与预览一致的大小（原因：前置摄像头识别出来的坐标会镜像翻转）
     * @param boundingBox
     * @return
     */
    public Rect getRealBoundingBox(Rect boundingBox) {
        Rect ret = new Rect(boundingBox);
        float x = translateX(boundingBox.centerX());
        float y = translateY(boundingBox.centerY());
        float xOffset = scaleX(boundingBox.width() / 2.0f);
        float yOffset = scaleY(boundingBox.height() / 2.0f);
        float left = x - xOffset;
        float top = y - yOffset;
        float right = x + xOffset;
        float bottom = y + yOffset;
        ret.left = (int) left;
        ret.top = (int) top;
        ret.right = (int) right;
        ret.bottom = (int) bottom;
        return ret;
    }

    /**
     * Adjusts a horizontal value of the supplied value from the preview scale to the view scale.
     */
    public float scaleX(float horizontal) {
        return horizontal * widthScaleFactor;
    }

    /** Adjusts a vertical value of the supplied value from the preview scale to the view scale. */
    public float scaleY(float vertical) {
        return vertical * heightScaleFactor;
    }

    /**
     * Adjusts the x coordinate from the preview's coordinate system to the view coordinate system.
     */
    public float translateX(float x) {
        if (mIsFrontCamera) {
            return mViewWidth - scaleX(x);
        } else {
            return scaleX(x);
        }
    }

    /**
     * Adjusts the y coordinate from the preview's coordinate system to the view coordinate system.
     */
    public float translateY(float y) {
        return scaleY(y);
    }

    /**
     * 关键点序列化
     * @param face
     * @return
     */
    public String landmark2Str(FirebaseVisionFace face) {
        String ret = "";
        if (face == null) {
            return ret;
        }
        FirebaseVisionFaceContour contour = face.getContour(FirebaseVisionFaceContour.ALL_POINTS);
        for (com.google.firebase.ml.vision.common.FirebaseVisionPoint point : contour.getPoints()) {
            float px = translateX(point.getX());
            float py = translateY(point.getY());

        }
        return ret;
    }

    public static String encrypt(String str) {
        return CryptTool.encrypt(str, new String(LANDMARK_ENCRYPT_KEY));
    }

}
